﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Globalization;
using WebUI.SmartDatingRef;
using SvcUtils;

namespace WebUI {
    public partial class Dashboard : BasePage {
        protected PersonalData pd;

        protected void Page_Load(object sender, EventArgs e) {
            if (!Context.User.Identity.IsAuthenticated) {
                FormsAuthentication.RedirectToLoginPage();
            }

            // получить персональную информацию о пользователе
            string sessionKey = SessionKey;
            string sessionKeyHash = SessionKeyHash;
            if (SessionKey == null) {
                pnlPersonalInfo.Visible = false;
                return;
            }

            ResultWithStatusOfstring resGetPd = Client.GetPersonalData(sessionKeyHash);

            Utils.hideError(errMsg);
            Utils.hideError(warningMsg);
            if (Status.SUCCESS(resGetPd.Status)) {
                pd = CryptoImpl.readObjectEncrypted<PersonalData>(b(resGetPd.Result), b(sessionKey));
            } else if (resGetPd.Status == OperationStatus.NoPersonalData) {
                // no personal data
                Utils.showError(warningMsg, Status.m(resGetPd.Status));
                pnlPersonalInfo.Parent.Controls.Remove(pnlPersonalInfo);
            } else {
                Utils.showError(errMsg, Status.m(resGetPd.Status));
                pnlPersonalInfo.Visible = false;
                return;
            }

            if (pd != null) {
                lblAge.Text = Math.Floor((DateTime.Today - pd.date_of_birth).TotalDays / 365).ToString();
            }

            if (pd != null && pd.zodiac.HasValue) {
                imgZodiac.Src = "Images/zodiac_" + pd.zodiac.Value.ToString() + ".png";
                imgZodiac.Alt = Utils.ZodiacToString(pd.zodiac.Value);
                lblZodiac.InnerText = imgZodiac.Alt;
            } else {
                imgZodiac.Visible = false;
                lblZodiac.Visible = false;
            }

            if (pd != null && pd.interests != null) {
                rptInterests.DataSource = pd.interests.Split(',');
            }

            // получить сообщения (если есть)
            ResultWithStatusOfArrayOfMsgswc4fAsa resMsgs =
                Client.GetPMs(sessionKeyHash, MessageFolder.Inbox);
            if (Status.SUCCESS(resMsgs.Status)) {
                const int PREVIEW_MESSAGE_COUNT = 5;
                lstMessages.DataSource = resMsgs.Result.Take(
                    Math.Min(PREVIEW_MESSAGE_COUNT, resMsgs.Result.Count())).ToArray();
                lstMessages.DataBind();
            }

            Page.DataBind();

            // получить местоположение (если доступно)
            ResultWithStatusOfNullableOfPairOfdoubledoubleswc4fAsa5F2dSckg resLoc =
                Client.GetLocation(sessionKeyHash);
            if (Status.SUCCESS(resLoc.Status)) {
                double latitude = resLoc.Result.Value.First;
                double longitude = resLoc.Result.Value.Second;

                CultureInfo cult = new CultureInfo("en-US");
                ScriptManager.RegisterClientScriptBlock(this, typeof(Dashboard), "maps",
                    "function loadMap() { initializeMap({ mapId: 'map', latitude: " + latitude.ToString(cult) + "," +
                    "longitude: " + longitude.ToString(cult) + ", gender: " + (pd != null ? pd.gender : 2) + "," +
                    "title: '" + lblName.Text + "', canMoveMyself: false, txtLatId: undefined, txtLongId: undefined});" +
                    "codeLatLng(" + latitude.ToString(cult) + ", " + longitude.ToString(cult) + "); }",
                    true);
            } else {
                ScriptManager.RegisterClientScriptBlock(this, typeof(Dashboard), "maps",
                    "function loadMap() { }",
                    true);
            }
        }
    }
}